var name = "";
var ip = "";
var port = "";
var players = "";
var maxPlayers = "";

function onClickJoinServer()
{
	var webId = "";
	var userName = "";
	var launch = true;
	//Check if a server has been selected
	if(ip != "" && port != ""){
		if((typeof $("#webid").val() === 'undefined')){
			launch = false;
		}
		else webId = $("#webid").val();
		
		if((typeof $("#username").val() === 'undefined')){
			launch = false;
		}
		else userName = $("#username").val()
		
		if(launch == true){
			LaunchGame(ip, port, userName, webId);
		}
		else alert("You need to log in order to use this feature.");
	}
}
	
function onClickServer(server)
{
	var id = $(server).attr("id");
	var data = $(server).attr("serverdata");
	var dataArray = data.split('#');
	var dataCount = dataArray.length
	if(dataCount >= 5)
	{
		if(dataCount > 5) //a # was used in the server name. Fix it.
		{
			var fixedNameData = "";
			for (var i=0;i<dataCount - 4;i++){
				if(i > 0){fixedNameData += "#";}
				fixedNameData += dataArray[i];
			}
			name = fixedNameData;
		}
		else{
			name = dataArray[0];
		}
		ip = dataArray[dataCount-4]; //dataCount can be different when a # is used in the name so compensate for that.
		port = dataArray[dataCount-3];
		players = dataArray[dataCount-2];
		maxPlayers = dataArray[dataCount-1]; //Last split item
	}
	deselect();
	
	$(server).addClass('isSelected');
	var children = server.getElementsByTagName('*');
	$(children[2]).addClass('isSelectedIP'); //remove border etc
	$(children[4]).addClass('isSelectedPort');
	$(children[6]).addClass('isSelectedPlayers');
	$(children[8]).addClass('isSelectedState');
	
	//View server details above the join button
	 $("#server-name").text(name);
}


function deselect()
{
	$(".server-item").each(function(){
        var item = this;
        $(item).removeClass('isSelected');
    });
	$(".server-item-ip").each(function(){
        var item = this;
        $(item).removeClass('isSelectedIP');
    });
	$(".server-item-port").each(function(){
        var item = this;
        $(item).removeClass('isSelectedPort');
    });
	$(".server-item-players").each(function(){
        var item = this;
        $(item).removeClass('isSelectedPlayers');
    });
}